var layer,
    form,
    upload,
    $;

var user = JSON.parse(localStorage.getItem('user'));
layui.use(['jquery', 'layer', 'form', 'upload'], function () {
    layer = layui.layer,
        upload = layui.upload,
        form = layui.form,
        $ = layui.jquery;

    var uploadInst = upload.render({
        elem: '#test1' //绑定元素
        ,url: '/Upload/uploadImages' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,auto:false
        ,bindAction:'#subUpload'
        // ,multiple: true
        ,accept: 'images'
        ,field: 'images'
        ,size: 1000
        ,choose:function (obj) {
            var files = obj.pushFile();

            obj.preview(function(index, file, result){
                $('#saveImg').append('<img src="' + result
                    + '" alt="' + file.name
                    + '"height="100px" width="100px" id="remove_'+index+'" class="layui-upload-img uploadImgPreView">');


                $('#remove_' + index).bind('dblclick', function () {//双击删除指定预上传图片
                    delete files[index];//删除指定图片
                    $(this).remove();
                });
            });

            // $('#showBtn').removeClass('layui-hide').append('<img src='+ res.files.file +'/>');
        }
        ,done: function(res){
            //上传完毕回调
            $('input[name = mainImage]').val(res.data[0]);
            console.log(res)
        }
        ,error: function(){
            //请求异常回调
        }
    });

    $('#test1').click(function(){
        $('#showBtn').html('<div class="layui-upload-list" id="saveImg" style="margin-left: 120px; "></div>');
    });

    //添加
    form.on('submit(formDemo)', function(data){
        //触发上传图片
        $('#subUpload').click();

        setTimeout(function () {
            //获取时间
            var date = getDate();
            data = form.val('form');
            data.productSate = 1;
            data.createId = user.userId;
            data.createDate = date;
            data.applyDate = date;
            data.launchId = user.userId;
            data.result = 2;
            data.type = 3;
            console.log(data);
            $.ajax({
                url:'/Product/addProduct',
                type: 'post',
                dataType:'json',
                data: data,
                success:function (data) {
                    if(data.data){
                        // 添加的操作日志
                        $.ajax({
                            url:'/Product/addProductOperationRecord',
                            dataType:'json',
                            data:{
                                "operationType": "1",
                                "executionTime": date,
                                "remarks": "添加商品信息",
                                "updateId": user.userId,
                                "operationList": 'js_product',
                                "type": 1
                            }
                        });
                        layer.msg("添加成功");
                    }else{
                        layer.msg("添加失败");
                    }
                }
            });
        }, 1000 )
        return false;
    });
});

function findBookName () {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '信息',
        resize: false,
        area: ['670px', '500px'],
        content: '/product/addProduct-layer/productlist-category.html'
    });
};

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
}

//刷新
function refresh() {
    window.location.reload();
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}